package com.example.demo1101.demo2;

public class SellTicket01 {

    public static void main(String[] args) throws InterruptedException {

        //多线程操作 多个线程操作同一个资源类
        Ticket01 ticket = new Ticket01();

        Thread t1 = new Thread(ticket, "A窗口");
        Thread t2 = new Thread(ticket, "B窗口");
        Thread t3 = new Thread(ticket, "C窗口");
        Thread t4 = new Thread(ticket, "D窗口");
        t1.start();
        t2.start();
        t2.join();
        t3.start();
        t4.start();


    }
}


class Ticket01 implements Runnable {
    //属性 方法
    private int number = 50;

    //买票的方式 synchronized本质就是排队 锁
    public synchronized void run() {


        for (int i = 0; i <= number + 2; i++) {
            System.out.println(Thread.currentThread().getName() + "卖出了第" + (50 - (--number)) + "票，剩余" + number);
        }
    }
}